//
//  AppDelegate.m
//  DWL_Vogue
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 Forlosan. All rights reserved.
//

#import "AppDelegate.h"
#import "ClothesViewController.h"
#import "FoodViewController.h"
#import "PlayViewController.h"
#import "MyLifeViewController.h"
#import "PersonViewController.h"
#import "WidthAndHeight.h"
#import "UMSocial.h"

#define SCREENWIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define SCREENHEIGH ([UIScreen mainScreen].bounds.size.height/667)

@interface AppDelegate ()<UITabBarControllerDelegate, UIScrollViewDelegate>
@property(nonatomic, retain)UIPageControl *page;

@end

@implementation AppDelegate
- (void)dealloc {
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        [self createView];
        
        
        UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
        myScroll.contentSize = CGSizeMake(self.window.frame.size.width * 3, self.window.frame.size.height);
        for (int i = 0; i < 3; i++) {
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width * i, 0, self.window.frame.size.width, self.window.frame.size.height)];
            
            myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d.jpg", i + 1]];
            [myScroll addSubview:myImage];
            
        }
        myScroll.pagingEnabled = YES;
        myScroll.showsHorizontalScrollIndicator = NO;
        myScroll.bounces = NO;
        [self.window addSubview:myScroll];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [myScroll addSubview:button];
        button.frame = CGRectMake(self.window.frame.size.width * 2 + self.window.frame.size.width / 2 - 80 * SCREENWIDTH, self.window.frame.size.height - 80 * SCREENHEIGH, 150, 40);
        [button setTitle:@"开启时尚之旅" forState:UIControlStateNormal];
        [button setTintColor:[UIColor whiteColor]];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor whiteColor].CGColor;
        button.layer.cornerRadius = 20;
        [button addTarget:self action:@selector(myAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        myScroll.delegate = self;
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 620 * SCREENHEIGH, self.window.frame.size.width, 30)];
        self.page.numberOfPages = 3;
        self.page.currentPage = 0;
        self.page.backgroundColor = [UIColor clearColor];
        self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
        self.page.pageIndicatorTintColor = [UIColor lightGrayColor];
        [self.window addSubview:self.page];
        
    } else {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        [self createView];
    }
    
    [UMSocialData setAppKey:@"563c14a267e58ebf8e0029f0"];
    return YES;
}

- (void)createView {
    
//    tabBar
    MyLifeViewController *lifeVC = [[MyLifeViewController alloc] init];
    UINavigationController *lifeNAVC = [[UINavigationController alloc] initWithRootViewController:lifeVC];
    lifeNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"生活" image:[UIImage imageNamed:@"life.png"] selectedImage:[UIImage imageNamed:@"life.png"]] autorelease];
    ClothesViewController *clothesVC = [[ClothesViewController alloc] init];
    UINavigationController *clothesNAVC = [[UINavigationController alloc] initWithRootViewController:clothesVC];
    clothesNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"衣" image:[UIImage imageNamed:@"clothes.png"] selectedImage:[UIImage imageNamed:@"clothes.png"]] autorelease];
    FoodViewController *foodVC = [[FoodViewController alloc] init];
    UINavigationController *foodNAVC = [[UINavigationController alloc] initWithRootViewController:foodVC];
    foodNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"食" image:[UIImage imageNamed:@"food.png"] selectedImage:[UIImage imageNamed:@"food.png"]] autorelease];
    PlayViewController *playVC = [[PlayViewController alloc] init];
    UINavigationController *playNAVC = [[UINavigationController alloc] initWithRootViewController:playVC];
    playNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"玩" image:[UIImage imageNamed:@"play.png"] selectedImage:[UIImage imageNamed:@"play.png"]] autorelease];
    PersonViewController *personVC = [[PersonViewController alloc] init];
    UINavigationController *personNAVC = [[UINavigationController alloc] initWithRootViewController:personVC];
    personNAVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"个人" image:[UIImage imageNamed:@"person.png"] selectedImage:[UIImage imageNamed:@"person.png"]] autorelease];
//    set
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = @[lifeNAVC, clothesNAVC, foodNAVC, playNAVC, personNAVC];
    self.window.rootViewController = tab;
    tab.tabBar.barTintColor = [UIColor colorWithRed:39 / 255.0 green:38 / 255.0 blue:55 / 255.0 alpha:1];
    tab.tabBar.tintColor =[UIColor whiteColor];
    tab.selectedIndex = 2;
    tab.delegate = self;
//    release
    [lifeVC release];
    [lifeNAVC release];
    [clothesVC release];
    [clothesNAVC release];
    [foodVC release];
    [foodNAVC release];
    [playVC release];
    [playNAVC release];
    [personVC release];
    [personNAVC release];
    [tab release];
    
//    创建缓存文件夹
    //  文件管理
    //  1. 找到沙盒路径
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    //    NSLog(@"%@", sandBoxPath);
    //    NSLog(@"%@", sandBox);
    
    //  创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //  要拼接一个文件夹的路径
    //  文件夹的名字没有拓展名
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"MyCaches"];
    
    //  根据路径创建一个文件夹
    [manager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:nil];
    
}

- (void)myAction
{
    [self createView];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    
    self.page.currentPage = scrollView.contentOffset.x / scrollView.window.frame.size.width;
    
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
